<?php
            App::import('Model', 'Cortestubo');
            $modeloCortes = new Cortestubo();

            App::import('Model', 'Movimientostrabajo');
            $modeloMovimientosTrabajo = new Movimientostrabajo();
            ?>
            <?php
$rol = $this->Session->read('Auth.User.role');
$sucursalUsuario = $this->Session->read('Auth.User.sucursale_id');
?>
<?php foreach ($cortesTubo as $ct): ?>
    <style>
        #element_to_pop_up_vende_<?php echo $ct['Cortestubo']['id']; ?> { 
            background-color:#fff;
            box-shadow: 0px 2px 3px rgba(0, 0, 0, 0.298);
            border-radius:5px;
            color:#000;
            display:none; 
            padding:5px;
            min-width:300px;
            min-height: 180px;        
        }      
    </style>

    <style>
        #element_to_pop_up_saca_<?php echo $ct['Cortestubo']['id']; ?> { 
            background-color:#fff;
            box-shadow: 0px 2px 3px rgba(0, 0, 0, 0.298);
            border-radius:5px;
            color:#000;
            display:none; 
            padding:5px;
            min-width:300px;
            min-height: 180px;        
        }
        .b-close{
            cursor:pointer;
            position:absolute;
            right:10px;
            top:5px;            
        }   
    </style>

    <style>
        #element_to_pop_up_corta_<?php echo $ct['Cortestubo']['id']; ?> { 
            background-color:#fff;
            box-shadow: 0px 2px 3px rgba(0, 0, 0, 0.298);
            border-radius:5px;
            color:#000;
            display:none; 
            padding:5px;
            min-width:325px;
            min-height: 180px;        
        }
        .b-close{
            cursor:pointer;
            position:absolute;
            right:10px;
            top:5px;            
        }   
    </style>

    <style>
        #element_to_pop_up_arbol_<?php echo $ct['Cortestubo']['id']; ?> { 
            background-color:#fff;
            box-shadow: 0px 2px 3px rgba(0, 0, 0, 0.298);
            border-radius:5px;
            color:#000;
            display:none; 
            padding:5px;
            min-width:300px;
            min-height: 180px;        
        }
        .b-close{
            cursor:pointer;
            position:absolute;
            right:10px;
            top:5px;            
        }   
    </style>

    <div id="element_to_pop_up_vende_<?php echo $ct['Cortestubo']['id']; ?>">
        <a class="b-close">X</a>                    
        <div class="widget grid12">
            <div class="whead"><h6> <?php echo $vender ?> </h6><div class="clear"></div></div>
            &nbsp;
            <h4>&nbsp;&nbsp; PRODUCTO: <?php echo $ct['Cortestubo']['codigo']; ?></h4>
            <?php if ($idCategoria == 19): ?>
                <h5>&nbsp;&nbsp; Alto: <?php echo $ct['Cortestubo']['alto']; ?> cm. &nbsp;&nbsp; Peso: <?php echo $ct['Cortestubo']['peso']; ?> Kilos</h5>
            <?php else: ?>
                <h5>&nbsp;&nbsp; &nbsp;&nbsp; Peso: <?php echo $ct['Cortestubo']['peso']; ?> Kilos</h5>
            <?php endif; ?>
            <?php echo $this->Form->create('Cortestubos', array('action' => 'venderbobina', 'id' => "validate")); ?>
            <div class="formRow">                                                                                                                
                <div class="grid3"><label>Cliente:<span class="req">*</span></label></div>
                <div class="grid3">
                    <?php echo $this->Form->text('cliente', array('style' => 'width: 150px;', 'required')); ?>
                    <?php echo $this->Form->hidden('cortestubo_id', array('value' => $ct['Cortestubo']['id'])); ?>
                    <?php echo $this->Form->hidden('tubo_id', array('value' => $ct['Tubo']['id'])); ?>
                </div>
            </div>    
            <div class="formRow">                                                                                                                
                <div class="grid3"><label>Precio:<span class="req">*</span></label></div>
                <div class="grid3">
                    <?php echo $this->Form->text('precio', array('style' => 'width: 80px;', 'required')); ?>                    
                </div>
            </div>   
            <div class="formRow">                                                                                                                
                <div class="grid3"><label>Propietario:<span class="req">*</span></label></div>
                <div class="grid3">
                    <?php $dcp = array('Fernando' => 'FERNANDO', 'General' => 'GENERAL INTERNACIONAL SRL.'); ?>
                    <?php echo $this->Form->select('propietario', $dcp, array('required')); ?>                  
                </div>
            </div>
            <table style="width: 122px; border: 0;">
                <tr>
                    <td style="border: 0; text-align: center;"> Nota Entrega&nbsp;&nbsp; <?php
                        echo
                        $this->Form->checkbox('ne');
                        ?></td>
                </tr>
                <tr>
                    <td style="border: 0;text-align: center;"> Nota Venta&nbsp;&nbsp;&nbsp;<?php
                        echo
                        $this->Form->checkbox('nv');
                        ?></td>    
                </tr>
            </table>
            <div class="clear"></div>
            <div class="formRow">
                <input type="submit" value="Vender Bobina" class="buttonS bGreen" /><div class="clear"></div>            
            </div>
            </form>

        </div>
    </div>

    <div id="element_to_pop_up_saca_<?php echo $ct['Cortestubo']['id']; ?>">
        <a class="b-close">X</a>                    
        <div class="widget grid12">
            <div class="whead"><h6>SALIDA DE ALMACEN </h6><div class="clear"></div></div>
            &nbsp;
            <h4>&nbsp;&nbsp; PRODUCTO: <?php echo $ct['Cortestubo']['codigo']; ?></h4>
            <?php if ($idCategoria == 19): ?>
                <h5>&nbsp;&nbsp; Alto: <?php echo $ct['Cortestubo']['alto']; ?> cm. &nbsp;&nbsp Peso: <?php echo $ct['Cortestubo']['peso']; ?> Kilos</h5>
            <?php else: ?>
                <h5>&nbsp;&nbsp; &nbsp;&nbsp Peso: <?php echo $ct['Cortestubo']['peso']; ?> Kilos</h5>
            <?php endif; ?>
            <?php echo $this->Form->create('Cortestubos', array('action' => 'sacarbobina', 'id' => "validate")); ?>
            <div class="formRow">                                                                                                                
                <div class="grid3"><label>Enviar A:<span class="req">*</span></label></div>
                <div class="grid3">
                    <?php echo $this->Form->select('sucursale_id', $dcs, array('required')); ?>
                    <?php echo $this->Form->hidden('tubo_id', array('value' => $ct['Cortestubo']['id'])); ?>
                </div>
            </div>    
            <div class="clear"></div>
            <div class="formRow">
                <input type="submit" value="Registrar Salida" class="buttonS bGreen" /><div class="clear"></div>            
            </div>
            </form>
        </div>
    </div>

    <div id="element_to_pop_up_arbol_<?php echo $ct['Cortestubo']['id']; ?>">
        <a class="b-close">X</a>  
        <div class="widget grid12">
            <div class="whead"><h6><?php echo $cortes; ?> </h6><div class="clear"></div></div>
            <div class="content<?php echo $ct['Cortestubo']['id']; ?>"></div>        
        </div>
    </div>

    <div id="element_to_pop_up_corta_<?php echo $ct['Cortestubo']['id']; ?>">
        <a class="b-close">X</a>                    
        <div class="widget grid12">
            <div class="whead"><h6><?php echo $dividir; ?>                          
                </h6><div class="clear"></div></div>
            &nbsp;
            <h4>&nbsp;&nbsp; CODIGO PRODUCTO: <?php echo $ct['Cortestubo']['codigo']; ?></h4>
            <?php if ($categoria == 19): ?>
                <h5>&nbsp;&nbsp; Alto: <?php echo $ct['Cortestubo']['alto']; ?> cm. &nbsp;&nbsp Peso: <?php echo $ct['Cortestubo']['peso']; ?> Kilos</h5>
            <?php else: ?>
                <h5>&nbsp;&nbsp; &nbsp;&nbsp Peso: <?php echo $ct['Cortestubo']['peso']; ?> Kilos</h5>
            <?php endif; ?>

            <?php echo $this->Form->create('Cortestubo', array('action' => 'registrasubcortes', 'id' => "validate")); ?>      
            <div class="formRow">   
                <div style="float: left;"><?php echo $di; ?> # 1: =>&nbsp;&nbsp;</div>
                <?php if ($categoria == 19): ?>
                    <div class="grid3" style="float: left;"><label>Alto:<span class="req">*</span></label></div>
                    <div class="grid3" style="width: 60px; float: left;">
                        <?php echo $this->Form->text('0.alto', array('size' => 4, 'required')); ?>                       
                    </div>
                <?php endif; ?>
                &nbsp;&nbsp;&nbsp;&nbsp;
                <div class="grid3" style="float: left;"><label>&nbsp;Peso:<span class="req">*</span></label></div>
                <div class="grid3" style="width: 60px; float: left;">
                    <?php echo $this->Form->text('0.peso', array('size' => 4, 'required')); ?>                                    
                    <?php echo $this->Form->hidden('0.tubo_id', array('value' => $ct['Cortestubo']['id'])); ?>
                    <?php echo $this->Form->hidden('0.categoria', array('value' => $categoria)); ?>                                                    
                </div>
            </div>  
            <div class="clear"></div>

            <div class="formRow">   
                <div style="float: left;"><?php echo $di; ?> # 2: =>&nbsp;&nbsp;</div>
                <?php if ($categoria == 19): ?>
                    <div class="grid3" style="float: left;"><label>Alto:<span class="req">*</span></label></div>
                    <div class="grid3" style="width: 60px; float: left;">
                        <?php echo $this->Form->text('1.alto', array('size' => 4, 'required')); ?>                                    
                    </div>
                <?php endif; ?>
                &nbsp;&nbsp;&nbsp;&nbsp;
                <div class="grid3" style="float: left;"><label>&nbsp;Peso:<span class="req">*</span></label></div>
                <div class="grid3" style="width: 60px; float: left;">
                    <?php echo $this->Form->text('1.peso', array('size' => 4, 'required')); ?>     
                    <?php echo $this->Form->hidden('1.tubo_id', array('value' => $ct['Cortestubo']['id'])); ?>
                    <?php echo $this->Form->hidden('1.categoria', array('value' => $categoria)); ?>                                                    
                </div>
            </div>  
            <div class="clear"></div>

            <div class="formRow">   
                <div style="float: left;"><?php echo $di; ?> # 3: =>&nbsp;&nbsp;</div>
                <?php if ($categoria == 19): ?>
                    <div class="grid3" style="float: left;"><label>Alto:<span class="req">*</span></label></div>
                    <div class="grid3" style="width: 60px; float: left;">
                        <?php echo $this->Form->text('2.alto', array('size' => 4)); ?>                                    
                    </div>
                <?php endif; ?>
                &nbsp;&nbsp;&nbsp;&nbsp;
                <div class="grid3" style="float: left;"><label>&nbsp;Peso:<span class="req">*</span></label></div>
                <div class="grid3" style="width: 60px; float: left;">
                    <?php echo $this->Form->text('2.peso', array('size' => 4)); ?>    
                    <?php echo $this->Form->hidden('2.tubo_id', array('value' => $ct['Cortestubo']['id'])); ?>
                    <?php echo $this->Form->hidden('2.categoria', array('value' => $categoria)); ?>                                                    
                </div>
            </div>  
            <div class="clear"></div>
            <div class="formRow">
                <input type="submit" value="<?php echo $dividir; ?>" class="buttonS bGreen" /><div class="clear"></div>            
            </div>
            </form>
        </div>
    </div>   

<?php endforeach; ?>
<table cellpadding="0" cellspacing="0" border="0" id="grid" class="tDefault">
                <thead>
                    <tr>   
                        <th style="width: 20px;">ID</th>                         
                        <th style="width: 90px;">CODIGO</th>  
                        <th>TIPO</th>  
                        <th>LUGAR</th>                        
                        <th style="width: 90px;">PADRE</th>                        
                        <?php if ($categoria == 19): ?>
                            <th>ALTO</th>          
                        <?php else: ?>
                        <?php endif; ?>
                        <th>PESO</th>
                        <th style="width: 130px;">Acciones</th>
                    </tr>
                </thead>

                <tfoot>
                    <tr>
                    <th rowspan="1" colspan="1">
                    <div class="formRow">
                        <span class="grid6">
                            <input type="text" name="search_cantidad" placeholder="ID" class="search_init" style="width: 25px;" />
                        </span>
                    </div>
                </th>
                <th rowspan="1" colspan="1">
                <div class="formRow">
                    <span class="grid6">
                        <input type="text" name="search_cantidad" placeholder="Codigo" class="search_init" style="width: 70px;" />
                    </span>
                </div>
                </th>
                <th rowspan="1" colspan="1">
                <div class="formRow">
                    <span class="grid6">
                        <input type="text" name="search_cantidad" placeholder="Tipo" class="search_init" style="width: 70px;" />
                    </span>
                </div>
                </th>
                <th rowspan="1" colspan="1">
                <div class="formRow">
                    <span class="grid6">
                        <input type="text" name="search_cantidad" placeholder="Lugar" class="search_init" />
                    </span>
                </div>
                </th>
                <th rowspan="1" colspan="1">
                <div class="formRow">
                    <span class="grid6">
                        <input type="text" name="search_cantidad" placeholder="Padre" class="search_init" style="width: 60px;" />
                    </span>
                </div>
                </th>
                <?php if ($categoria == 19): ?>
                    <th rowspan="1" colspan="1">
                    <div class="formRow">
                        <span class="grid6">
                            <input type="text" name="search_cantidad" placeholder="Alto" class="search_init" />
                        </span>
                    </div>
                    </th>
                <?php else: ?>
                <?php endif; ?>
                <th rowspan="1" colspan="1">
                <div class="formRow">
                    <span class="grid6">
                        <input type="text" name="search_alto" placeholder="Peso" class="search_init" />
                    </span>
                </div>
                </th>  
                <th rowspan="1" colspan="1">
                <div class="formRow">
                    <span class="grid6">
                        <input type="text" name="search_cantidad" placeholder="" class="search_init" />
                    </span>
                </div>
                </th>
                </tr>
                </tfoot>
                <tbody>
                    <?php //$i = 1;     ?>                
                    <?php foreach ($cortesTubo as $ct): ?>                       
                        <?php $idCodigo = $ct['Cortestubo']['id']; ?>
                        <tr>   
                            <td>
                                <?php echo $ct['Cortestubo']['id']; ?>                                
                            </td>

                            <td>
                                <h3><?php echo $ct['Cortestubo']['codigo']; ?></h3>
                                <h6><?php echo $alto = ($categoria == 19) ? $ct['Tubo']['micraje'] . ' Micras' : ''; ?></h6>
                            </td>
                            <td>
                                <?php echo $ct['Tubo']['nombre']; ?>
                            </td>
                            <td>
                                <?php echo $ct['Sucursale']['nombre']; ?>                                
                            </td>
                            <td>
                                <?php
                                $padreNodo = $modeloCortes->getParentNode($ct['Cortestubo']['id']);
                                //echo "<b>" . $padreNodo['Cortestubo']['codigo'] . "</b>";
                                $codigoPadre = $padreNodo['Cortestubo']['codigo'];
                                if (empty($codigoPadre)) {
                                    echo $padreNodo['Cortestubo']['id'];
                                } else {
                                    echo $padreNodo['Cortestubo']['codigo'];
                                }
                                //debug($padreNodo);
                                ?>
                            </td> 
                            <?php if ($categoria == 19): ?>
                                <td><?php echo $ct['Cortestubo']['alto']; ?> cm.</td>
                            <?php else: ?>
                            <?php endif; ?>
                            <td>
                                <?php
                                //echo $ct['Cortestubo']['peso'];
                                $ultimoPeso = $modeloMovimientosTrabajo->find('first', array(
                                    'recursive' => -1,
                                    'conditions' => array('cortestubo_id' => $ct['Cortestubo']['id']),
                                    'order' => 'id DESC'
                                ));
                                //debug($ultimoPeso);
                                if (!empty($ultimoPeso)) {
                                    echo $ultimoPeso['Movimientostrabajo']['sobra'];
                                } else {
                                    echo $ct['Cortestubo']['peso'];
                                }
                                ?> Kilos
                            </td> 
                            <td>
                                <?php if ($rol == 'admin'): ?>
                                    <a onclick="ajaxSalida<?php echo $ct['Cortestubo']['id']; ?>();"><?php echo $this->Html->image("iconos/salida.png", array('title' => $sacar)) ?></a>

                                    <a onclick="ajaxCorta<?php echo $ct['Cortestubo']['id']; ?>();"><?php echo $this->Html->image("iconos/corte.png", array('title' => $nombrecategoria)) ?></a>

                                    <a onclick="ajaxArbol<?php echo $ct['Cortestubo']['id']; ?>();"><?php echo $this->Html->image("iconos/listaInv.png", array('title' => $ver)) ?></a>                            

                                    <a onclick="ajaxVende<?php echo $ct['Cortestubo']['id']; ?>();"><?php echo $this->Html->image("iconos/money.png", array('title' => $vender)) ?></a>
                                    <a href="<?php echo $this->Html->url(array('controller' => 'Cortestubos', 'action' => 'edit', $ct['Cortestubo']['id'])); ?>"><?php echo $this->Html->image('iconos/edit.png', array('title' => $ed)) ?></a>                                    
                                    <?php $codigoTubo = $ct['Cortestubo']['codigo']; ?>
                                    <?php
                                    echo $this->Form->postLink(
                                            $this->Html->image('iconos/baja2.png', array(
                                                "title" => 'Dar de Baja'
                                            )), array(
                                        'action' => 'darbaja',
                                        $ct['Cortestubo']['id']), array(
                                        'escape' => false,
                                        'confirm' => "Estas Seguro de dar baja a $codigoTubo?")
                                    );
                                    ?>
                                    <a href="<?php echo $this->Html->url(array('controller' => 'Cortestubos', 'action' => 'qr', $ct['Cortestubo']['id'])); ?>"><?php echo $this->Html->image('iconos/qr.png', array('title' => 'QR del producto')) ?></a>                                    
                                <?php else: ?>
                                <?php endif; ?>
                            </td>
                        </tr>                    
                    <?php endforeach; ?>               
                </tbody>               
            </table>
            <?php foreach ($cortesTubo as $ct): ?>
    <script>

        function ajaxVende<?php echo $ct['Cortestubo']['id']; ?>()
        {
            // Binding a click event
            // From jQuery v.1.7.0 use .on() instead of .bind()
            //$('#btAjaxGeneraSalida_<?php //echo $ct['Cortestubo']['id'];               ?>').bind('click', function(e) {
            console.log('click');
            // Triggering bPopup when click event is fired
            $('#element_to_pop_up_vende_<?php echo $ct['Cortestubo']['id']; ?>').bPopup({
                fadeSpeed: 'slow', //can be a string ('slow'/'fast') or int
                followSpeed: 1500, //can be a string ('slow'/'fast') or int                                             
                //$('#element_to_pop_up_<?php //echo $t['Tubo']['id'];                       ?>').bPopup()
            });
            //});    
        }

        function ajaxSalida<?php echo $ct['Cortestubo']['id']; ?>()
        {
            // Binding a click event
            // From jQuery v.1.7.0 use .on() instead of .bind()
            //$('#btAjaxGeneraSalida_<?php //echo $ct['Cortestubo']['id'];               ?>').bind('click', function(e) {
            console.log('click');
            // Triggering bPopup when click event is fired
            $('#element_to_pop_up_saca_<?php echo $ct['Cortestubo']['id']; ?>').bPopup({
                fadeSpeed: 'slow', //can be a string ('slow'/'fast') or int
                followSpeed: 1500, //can be a string ('slow'/'fast') or int                                             
                //$('#element_to_pop_up_<?php //echo $t['Tubo']['id'];                       ?>').bPopup()
            });
            //});    
        }

        function ajaxCorta<?php echo $ct['Cortestubo']['id']; ?>()
        {
            // Triggering bPopup when click event is fired
            $('#element_to_pop_up_corta_<?php echo $ct['Cortestubo']['id']; ?>').bPopup({
                fadeSpeed: 'slow', //can be a string ('slow'/'fast') or int
                followSpeed: 1500, //can be a string ('slow'/'fast') or int                                             
                //$('#element_to_pop_up_<?php //echo $t['Tubo']['id'];                      ?>').bPopup()
            });
        }

        function ajaxArbol<?php echo $ct['Cortestubo']['id']; ?>()
        {
            $('#element_to_pop_up_arbol_<?php echo $ct['Cortestubo']['id']; ?>').bPopup({
                fadeSpeed: 'slow', //can be a string ('slow'/'fast') or int
                followSpeed: 1500, //can be a string ('slow'/'fast') or int                                            
                contentContainer: '.content<?php echo $ct['Cortestubo']['id']; ?>',
                loadUrl: "<?php echo $this->Html->url(array('controller' => 'Cortestubos', 'action' => 'ajaxverarbol', $ct['Cortestubo']['id'])); ?>", //Uses jQuery.load()                                            
            });
        }

    </script>

<?php endforeach; ?> 